package com.example.service.customer.controller;

import com.example.service.api.dto.CustomerDTO;
import com.example.service.customer.service.CustomerService;
import com.example.stack.common.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
@author SupUncleBlack
@date 2021/8/27 - 14:38
*/
@RefreshScope
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @Value("${customer.config:xxx}")
    private String config;

    @GetMapping("/config")
    public String getConfig(){
        System.out.println(config);
        return config;
    }

    /**
     * 创建客户信息
     *
     * @param customerDTO 客户信息
     * @return 客户信息
     */
    @PostMapping
    public ResultEntity<CustomerDTO> createCustomerInfo(@RequestBody CustomerDTO customerDTO) {
        CustomerDTO vo = customerService.insertCustomer(customerDTO);
        return ResultEntity.successWithData(vo);
    }

    /**
     * 删除客户新信息
     *
     * @param id 客户号
     * @return 客户信息
     */
    @DeleteMapping("/{id}")
    public ResultEntity<CustomerDTO> deleteCustomerInfo(@PathVariable("id") String id) {
        customerService.deleteCustomerInfo(id);
        return ResultEntity.successWithoutData();
    }

    /**
     * 更新客户新信息
     *
     * @param customerDTO 客户信息
     * @return 客户信息
     */
    @PutMapping()
    public ResultEntity<CustomerDTO> updateCustomerInfo(@RequestBody CustomerDTO customerDTO) {
        CustomerDTO customerInfo = customerService.updateCustomerInfo(customerDTO);
        return ResultEntity.successWithData(customerInfo);
    }

    @GetMapping("/{customerId}")
    public ResultEntity<CustomerDTO> getCustomerInfo (@PathVariable("customerId") String customerId) {
        CustomerDTO customerInfo = customerService.getCustomerById(customerId);
        return ResultEntity.successWithData(customerInfo);
    }
}
